<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");
class IndexController extends Controller {


    public function _initialize()
    {
        if(!check_login()){
            $this->error('登录状态异常,请重新登录','/Admin/Login/login');
        }
        //对过期的停车位删除 和 恢复
        $lists  =   M('cards')->where(array('type'=>'1'))->select();
        foreach($lists as $k => $v){
            $time  = date('Y-m-d');
            $add_time   =  date("Y-m-d", strtotime("+{$v['mon']}months", strtotime($v['add_time'])));
            if($time > $add_time){ //超过时间
                M('seat')->where(array('seat_id'=>$v['seat_id']))->save(array('type'=>'0'));
                M('cards')->where(array('id'=>$v['id']))->save(array('type'=>'0'));
            }
        }
    }

    //修改密码
    public function passDo(){
        $oldPass    =   I('post.oldPass');
        $newPass    =   I('post.newpass');
        $renewPass  =   I('post.renewpass');
        if($newPass != $renewPass){
//            die("<script>alert('两次密码不一致');history.back(-1);</script>");
            $this->error("两次密码不一致");
        }
        $admin      =   session('user');
        $passYz     =   M('admin')->where(array('name'=>$admin))->getField('pwd');

        if($passYz != md5($oldPass)){
//            die("<script>alert('原始密码错误');history.back(-1);</script>");
            $this->error("原始密码错误");
        }
        $data['pwd']    =   md5($newPass);
        $res    =   M('admin')->where(array('name'=>$admin))->save($data);
        if($res !== false){
            $this->success('修改成功');
        }else $this->error("修改失败");

    }


    //删除数据
    public function delData()
    {
        $id     =   I('get.id');
        $table  =   I('get.table');
        $info   =   M($table)->where("id in ($id)")->find();

        $res    =   M($table)->where("id in ($id)")->delete();
        if($res !== false){
            if($info['pic']) unlink('.'.$info['pic']);
            if($info['mini_pic']) unlink('.'.$info['mini_pic']);
        }


        echo $res;
    }


}